#ifdef __APPLE__
#include <SDL/SDL.h>
#else
#include <SDL.h>
#endif

#include <SDL_image.h>
#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;

#include "ship.h"
#include "world.h"
#include "target.h"

int main ( int argc, char** argv )
{
    // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

	srand(time(NULL));

    // create a new window
    SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
                                           SDL_HWSURFACE|SDL_DOUBLEBUF);
    if ( !screen )
    {
        printf("Unable to set 640x480 video: %s\n", SDL_GetError());
        return 1;
    }

    // load an image
    SDL_Surface* bmp = IMG_Load("ship_sprite.png");
    SDL_Surface* projectile = IMG_Load("projectile.png");
	SDL_Surface* target = IMG_Load("target.png");

    if (!bmp || ! projectile || !target)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return 1;
    }
	bool draw_borders = false;

	World* world = new World(640, 480, 1,  1);
	Ship* ship = new Ship(Vect2D(50,50),bmp,world);
	ship->Set_projectile(projectile);
	world->Add_object(ship);


	bool skip_frame;
	int present_time = SDL_GetTicks(), past_time, time_diff, frames_skipped = 0;
    // program main loop
    bool done = false;
    while (!done)
    {
    	past_time = present_time;
    	present_time = SDL_GetTicks();
    	time_diff = present_time-past_time;
    	if(time_diff < 40)
    	{
			SDL_Delay(40-time_diff);
			frames_skipped = 0;
		}else
		{
			skip_frame = true;
			frames_skipped ++;
		}

        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event))
        {
            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                done = true;
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = true;
                    if(event.key.keysym.sym == SDLK_UP)
                    {
                    	ship->Move(1);
                    }else
                    if(event.key.keysym.sym == SDLK_DOWN)
                    {
                    	ship->Move(4);
                    }else
                    if(event.key.keysym.sym == SDLK_LEFT)
                    {
                    	ship->Move(8);
                    }else
                    if(event.key.keysym.sym == SDLK_RIGHT)
                    {
                    	ship->Move(2);
                    }else
                    if(event.key.keysym.sym == SDLK_b)
                    {
                    	draw_borders = ! draw_borders;
                    }else
                    if(event.key.keysym.sym == SDLK_s)	// print out status
                    {
                    	cout<<"Number of objects: "<<world->Get_number_objects()<<endl;
                    	cout<<endl;
                    }
                    break;
                }
			 case SDL_KEYUP:
                {
                    if(event.key.keysym.sym == SDLK_UP)
                    {
                    	ship->Move(1);
                    }else
                    if(event.key.keysym.sym == SDLK_DOWN)
                    {
                    	ship->Move(4);
                    }else
                    if(event.key.keysym.sym == SDLK_LEFT)
                    {
                    	ship->Move(8);
                    }else
                    if(event.key.keysym.sym == SDLK_RIGHT)
                    {
                    	ship->Move(2);
                    }else
                    if(event.key.keysym.sym == SDLK_SPACE)
                    {
						ship->Shot();
                    }
                    break;
                }
            } // end switch
        } // end of message processing

		Target::Target_generate(world, 0.5, target);

		world->Frame_calculate();
        // DRAWING STARTS HERE
		if(!skip_frame)
		{
			// clear screen
			SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));

			// draw bitmap
			world->Draw(screen, Vect2D(0,0));
			if(draw_borders)
				world->Draw_borders(screen, Vect2D(0,0), 0x0000ff99);//SDL_MapRGB(screen->format,0,0,255,255));
			// DRAWING ENDS HERE

			// finally, update the screen :)
			SDL_Flip(screen);
		}else
			skip_frame = false;
    } // end main loop

    // free loaded bitmap
    SDL_FreeSurface(bmp);
    SDL_FreeSurface(projectile);
    SDL_FreeSurface(target);

    // all is well
	return 0;
}
